<?php

namespace Modules\ShopAdmin\Models;
use Modules\AuthAdmin\Services\QueryService;
use Modules\AuthAdmin\Services\CommonService;
use Modules\AuthAdmin\Models\AuthImage as AuthImageModel;
class ShopOrderEvaluate extends BaseApiModel
{
	/**
     * @name  关联用户
     */
    public function user_to()
    {
        return $this->belongsTo('Modules\ShopAdmin\Models\ShopUser','user_id','id');
    }
	/**
     * @name  获取评论星级列表
     */
	private function level_list(){
		return [
			'1'=>'非常差',
			'2'=>'差',
			'3'=>'一般',
			'4'=>'好',
			'5'=>'非常好'
		];
	}
	/**
     * @name   状态0和1修改为true或false
     * @param  int  $value
     * @return Boolean 
     */
    public function getStatusAttribute($value)
    {
        return $value==1?true:false;
    }
	/**
	 * @name  商品列表
	 * @param  data array 搜索数据
	 * @return  JSON
	 */
	public function evaluate_list($data){
		$model = $this->query();
		$model = (new QueryService())->query_condition($model,$data);
		$list = $model->select('id','user_id','content','images','level','status','created_at')
				->whereHas('user_to',function($query)use ($data){
					if(!empty($data['nick_name'])){
						$query->where('nick_name', 'like', '%' . $data['nick_name'] . '%');
					}
				})
				->with([
					'user_to'=>function($query){
						$query->select('id','nick_name');
					}
				])
				->orderBy('id','desc')
				->where(['product_id'=>$data['id']])
				->paginate($data['pagesize'])
				->toArray();
		$level_list = $this->level_list();		
		$http = (new CommonService())->get_http_type();	
		foreach($list['data'] as $k=>$v){
			$list['data'][$k]['level_name'] = $level_list[$v['level']];
			if($v['images']){
				$list['data'][$k]['images'] = AuthImageModel::whereIn('id',explode('|',$v['images']))->select('id','url','open')->get();	
				if($list['data'][$k]['images']){
					$list['data'][$k]['images'] = $list['data'][$k]['images']->toArray();
					foreach($list['data'][$k]['images'] as $k1=>$v1){
						if($v1['open'] == 1){
							$list['data'][$k]['images'][$k1] = $http . $v1['url'];
						}else{
							$list['data'][$k]['images'][$k1] = $v1['url'];
						}
					}
				}else{
					$list['data'][$k]['images'] = [];
				}
			}else{
				$list['data'][$k]['images'] = [];
			}
		}		
		return $this->api_success(['data'=>[
			'list'=>$list['data'],
			'total'=>$list['total']
		]]);
	}
}
